// src/pages/catering/orderDetail/OrderDetail.js
import getIn from '../../../../yali-sdk/utils/getIn';
import fetchOrderDetail from '../../../../yali-sdk/business/order/fetchOrderDetail.js';
import fetchUpdateOrder from '../../../../yali-sdk/business/order/fetchUpdateOrder.js';
import { notify } from '../../../../yali-sdk/utils/NotifyRegistryCenter';
import { CATERINNG_GOODS_LIST } from '../../../pages/pathUrl';
import {
  ORDER_TYPE_UNPAID, // 未付款
  ORDER_TYPE_PAID, // 已付款
  ORDER_TYPE_SHIPPED, // 已发货 
  ORDER_TYPE_COMPLETED, // 已完成
  ORDER_TYPE_CANCELED, // 已取消
  ORDER_TYPE_CLOSED, // 已关闭
  ORDER_TYPE_AFTERMARKETING, // 售后中 
  ORDER_TYPE_PENDING, // 配餐中
  ORDER_TYPE_CATERING, // 餐饮
  ORDER_TYPE_HOTEL, // 酒店
  ORDER_TYPE_OTHER, // 其他
} from '../../../../yali-sdk/business/order/constrants.js';
import { getImagePath, showFail, showSuccess } from '../../../config/common';
import { getLocalTime } from '../../../config/dateTools';
import { SUCCESS } from '../../../../yali-sdk/utils/success';
import { FAIL } from '../../../../yali-sdk/utils/fail';
import { wxPay } from '../../../common/wxPayOrder';
import config from '../../../config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    orderDetailObject: {},
    shop_detail: {},
    ORDER_TYPE_UNPAID, // 未付款
    ORDER_TYPE_PAID, // 商家未接单
    ORDER_TYPE_SHIPPED, // 商家已接单
    ORDER_TYPE_COMPLETED, // 已完成
    ORDER_TYPE_CANCELED, // 已取消
    ORDER_TYPE_CLOSED, // 已关闭
    ORDER_TYPE_AFTERMARKETING, // 售后中 
    ORDER_TYPE_PENDING, // 配餐中
    ORDER_TYPE_CATERING, // 外卖
    ORDER_TYPE_HOTEL, // 酒店
    ORDER_TYPE_OTHER, // 其他
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const id = getIn(options, ['order_id']);
    const shop_id = getIn(options, ['shop_id']);
    wx.showLoading({ title: '加载中' });
    this.fetchData({ id, shop_id });
  },
  fetchData(params) {
    const token = getIn(getApp(), ['globalData', 'token']);
    global.ActionDispatcher.execute(fetchOrderDetail, { token, ...params }, (code, res) => {
      if (code == SUCCESS) {
        wx.hideLoading();
        const orderDetailObject = getIn(res, ['data']);
        const link_products = orderDetailObject && orderDetailObject.link_products;
        // let orderList = [];
        // Object.keys(link_products).map(x => {
        //   orderList = orderList.concat(link_products[x].list);
        // });
        this.setData({ orderDetailObject, orderList: link_products })
      } else if (code == FAIL) {
        wx.hideLoading();
      }
    });
  },
  // 拨打商家电话
  makePhone() {
    const { orderDetailObject:{shop} } = this.data;
    wx.makePhoneCall({
      phoneNumber: shop.tel
    })
  },
  // 复制订单编号
  copy() {
    wx.setClipboardData({
      data: this.data.orderDetailObject.order_no,
      success: (res) => {
        showSuccess('操作成功');
      },
      fail: () => {
        showFail('操作失败');
      }
    })
  },
  // 跳转点餐页
  toGoodList() {
    const { orderDetailObject } = this.data;
    const queryString = `table_no=${orderDetailObject.table_no}&type=${orderDetailObject.order_type}`;
    global.navigateHelper && global.navigateHelper.navigateTo({ pathname: CATERINNG_GOODS_LIST, queryString });
  },
  // 立即支付
  pay() {
    const token = getIn(getApp(), ['globalData', 'token']);
    const openid = getIn(getApp(), ['globalData', 'openid']);
    const order_id = this.data.orderDetailObject.id;
    const pay_way = 2;
    const trade_type = 'JSAPI';
    const domain = getIn(config, ['domain']);
    const queryString = `order_id=${order_id}`;
    const payParams = {
      token, order_id, pay_way, trade_type, domain, openid,
    };
    wxPay(payParams, (status, result) => {
      if (status == SUCCESS) {
        showSuccess('支付成功');
        this.fetchData({ id: order_id });
        notify('updateOrderList');
      } else if (status == 'PAY_FAIL') {
        showFail('支付失败');
      } else if (status == FAIL) {
        showFail(result && result.msg);
      }
    });
  },
  // 取消订单
  cancel() {
    const token = getIn(getApp(), ['globalData', 'token']);
    const id = this.data.orderDetailObject.id;
    wx.showModal({
      title: '确定取消订单？',
      success: (res) => {
        if (res.confirm) {
          global.ActionDispatcher.execute(fetchUpdateOrder, { token, id, status: ORDER_TYPE_CANCELED }, (code, res) => {
            if (code == SUCCESS) {
              showSuccess('取消成功');
              this.fetchData({ id });
            } else if (code == FAIL) {
              showFail('取消失败');
            }
          })
        }
      }
    })
  }
})